home *** CD-ROM | disk | FTP | other *** search
/ PC Media 2 / PC MEDIA CD02.iso / share / prog / realasm1 / ft2st.asm < prev    next >
Encoding:
Assembly Source File  |  1993-07-18  |  5.1 KB  |  123 lines

  1. .286
  2. ;=======================================
  3. ; invoke ft2st, real, string
  4. ;
  5. ; Convert a REAL10 to an ascii string.
  6. ; Maximum string length is 42 bytes plus null.
  7. ;------------------------------------------------
  8. cseg          segment word public 'code'
  9.               assume  cs:cseg,ss:cseg
  10.               assume  ds:cseg,es:cseg
  11.  
  12.               include math.inc
  13.  
  14. ft2st         proc    near real:NPR10, string:NPB
  15.               local   t:REAL10, one:REAL10, big:REAL10
  16.  
  17.               pusha                             ;
  18.               mov     si, real                  ; address real
  19.               invoke  movx, addr t, si, 5       ; make copy (t) of real
  20.               lea     si, t                     ; address copy
  21.  
  22.               mov     di, string                ; address string
  23.               cld                               ; forward
  24.  
  25.               invoke  ftzero, si                ; check for zero
  26.               .IF ax == TRUE                    ;
  27.                  mov     ax, '0+'               ;
  28.                  stosw                          ;
  29.                  mov     ax, '0.'               ;
  30.                  stosw                          ;
  31.                  xor     al, al                 ;
  32.                  stosb                          ; string = '+0.0',0
  33.                  jmp     exit                   ;
  34.               .ENDIF                            ;
  35.  
  36.               invoke  load10_19, addr big       ; load 10**19
  37.               invoke  load1, addr one           ; load 1.0
  38.  
  39.               mov     ax, word ptr [si]+8       ;
  40.               and     ax, 8000h                 ;
  41.               .IF  ax == 8000h                  ; if negative
  42.                  mov     al, '-'                ;
  43.                  and     word ptr [si]+8, 7fffh ;
  44.               .ELSE                             ; if positive
  45.                  mov     al, '+'                ;
  46.               .ENDIF                            ;
  47.               stosb                             ; write string
  48. ;------------------------------------------------
  49. ; process integers
  50. ;------------------------------------------------
  51.               mov     cx, 20                    ; 10**19 thru 10**0
  52.               xor     dx, dx                    ;
  53.  
  54.               .WHILE (cx)                       ;
  55.                  invoke  ftmod, si, addr big
  56.                  .IF (ax)                       ; if ax > 0
  57.                     or      dx, 1               ;
  58.                  .ENDIF                         ;
  59.  
  60.                  .IF (dx)                       ;
  61.                     add     al, '0'             ; convert to ascii
  62.                     stosb                       ; write string
  63.                  .ENDIF                         ;
  64.  
  65.                  invoke  ftdivi, addr big, +10  ; 10**N / 10.0
  66.                  dec     cx                     ; continue
  67.               .ENDW
  68. ;------------------------------------------------
  69. ; add decimal point
  70. ;------------------------------------------------
  71.               mov     bx, di                    ; save posn of dec point
  72.               .IF (dx)                          ;
  73.                  mov     al, '.'                ;
  74.                  stosb                          ;
  75.               .ELSE                             ;
  76.                  mov     ax, '.0'               ;
  77.                  stosw                          ;
  78.               .ENDIF                            ;
  79. ;------------------------------------------------
  80. ; process decimals
  81. ;------------------------------------------------
  82.               mov     cx, 20                    ;
  83.               .WHILE (cx)                       ;
  84.                  invoke  ftzero, si             ;
  85.                  .BREAK .IF (ax == TRUE)        ;
  86.  
  87.                  invoke  ftmuli, si, +10        ; binary * 10
  88.                  invoke  ftmod, si, addr one    ;
  89.                  add     al, '0'                ;
  90.                  stosb                          ;
  91.                  dec     cx                     ;
  92.               .ENDW                             ;
  93.  
  94.               xor     al, al                    ; terminate string
  95.               stosb                             ;
  96. ;------------------------------------------------
  97. ; round up decimals if '99' occurs
  98. ;------------------------------------------------
  99.               inc     bx                        ; skip dec point
  100.               mov     cx, 20                    ;
  101.               .WHILE (cx)                       ;
  102.                  lodsb                          ;
  103.                  .BREAK .IF (al == 0)           ; exit if string end
  104.  
  105.                  .IF ((byte ptr [bx]-1 != '9') \
  106.                    && (byte ptr [bx]+0 == '9') \
  107.                    && (byte ptr [bx]+1 == '9'))
  108.                     inc     byte ptr [bx]-1     ; + 1
  109.                     mov     byte ptr [bx], 0    ; terminate string
  110.                     .BREAK                      ; exit loop
  111.                  .ENDIF                         ;
  112.  
  113.                  dec    cx                      ; continue
  114.               .ENDW
  115. exit:
  116.               popa
  117.  
  118.               ret
  119. ft2st         endp
  120.  
  121. cseg          ends
  122.               end
  123.